package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import DataStorageClasses.Job;
import Database.DatabaseConnector;

@WebServlet("/GetJobListServlet")
public class GetJobsListServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	{
		DatabaseConnector db = new DatabaseConnector();
		
		String business = (String) request.getSession().getAttribute("username");
		int checked = Integer.parseInt(request.getParameter("checked"));
		
		String lowEstStart = request.getParameter("estStartLow");
		String highEstStart = request.getParameter("estStartHigh");
		String lowActStart = request.getParameter("actStartLow");
		String highActStart = request.getParameter("actStartHigh");
		String lowEstFinish = request.getParameter("estFinishLow");
		String highEstFinish = request.getParameter("estFinishHigh");
		String lowActFinish = request.getParameter("actFinishLow");
		String highActFinish = request.getParameter("actFinishHigh");
		
		Date lowEstStartDate = null, highEstStartDate=null, lowActStartDate=null, highActStartDate=null, lowEstFinishDate=null, 
				highEstFinishDate=null, lowActFinishDate=null, highActFinishDate=null;
		
		try {
			try {
				Calendar calendar = Calendar.getInstance();
				if(lowEstStart != null) {
					String[] date = lowEstStart.split("-");
					calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1])-1, Integer.parseInt(date[2]));
					lowEstStartDate = new Date(calendar.getTimeInMillis());
				}
				
				if(highEstStart != null) {
					String[] date = highEstStart.split("-");
					calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1])-1, Integer.parseInt(date[2]));
					highEstStartDate = new Date(calendar.getTimeInMillis());
				}
				
				if(lowActStart != null) {
					String[] date = lowActStart.split("-");
					calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1])-1, Integer.parseInt(date[2]));
					lowActStartDate = new Date(calendar.getTimeInMillis());
				}
				
				if(highActStart != null) {
					String[] date = highActStart.split("-");
					calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1])-1, Integer.parseInt(date[2]));
					highActStartDate = new Date(calendar.getTimeInMillis());
				}
				
				if(lowEstFinish != null) {
					String[] date = lowEstFinish.split("-");
					calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1])-1, Integer.parseInt(date[2]));
					lowEstFinishDate = new Date(calendar.getTimeInMillis());
				}
				
				if(highEstFinish != null) {
					String[] date = highEstFinish.split("-");
					calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1])-1, Integer.parseInt(date[2]));
					highEstFinishDate = new Date(calendar.getTimeInMillis());
				}
				
				if(lowActFinish != null) {
					String[] date = lowActFinish.split("-");
					calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1])-1, Integer.parseInt(date[2]));
					lowActFinishDate = new Date(calendar.getTimeInMillis());
				}
				
				if(highActFinish != null) {
					String[] date = highActFinish.split("-");
					calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1])-1, Integer.parseInt(date[2]));
					highActFinishDate = new Date(calendar.getTimeInMillis());
				}
			}
			catch (NumberFormatException e) {
				JSONObject result = new JSONObject();
				result.put("failed", true);
				result.put("message", "Incorrect Date Format");
				
				response.getWriter().print(result);
				response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
				return;
			}
			
			ArrayList<Job> jobs = db.getJobsBetweenDates(business, lowEstStartDate, highEstStartDate, lowActStartDate, highActStartDate, lowEstFinishDate, highEstFinishDate, lowActFinishDate, highActFinishDate, checked);
			JSONArray jsonArray = new JSONArray();
			for (Job job: jobs) {
				jsonArray.put(job.getJSONObject());
			}
			
			JSONObject result = new JSONObject();
			result.put("jobs", jsonArray);
			response.getWriter().print(result);
			
		} catch(JSONException e) { 
			e.printStackTrace();
		} catch(IOException e) {
			e.printStackTrace();
		}
		
		response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
	}
}
